package com.studentmanager.controller;

import com.studentmanager.domain.Activity;
import com.studentmanager.service.ActivityService;
import com.studentmanager.util.AjaxResult;
import com.studentmanager.util.Data;
import com.studentmanager.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    @GetMapping("/activity_list")
    public String activityList(){
        return "activity/activityList";
    }


    @PostMapping("/getActivityList")
    @ResponseBody
    public Object getActivityList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                  @RequestParam(value = "rows", defaultValue = "100") Integer rows,
                                  String name,
                                  String category,
                                  @RequestParam(value = "creator", defaultValue = "0") String creator,
                                  String from) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("pageno", page);
        paramMap.put("pagesize", rows);
        if (name != null && !name.isEmpty()) paramMap.put("name", name);
        if (category != null && !category.isEmpty()) paramMap.put("category", category);
        if (!creator.equals("0")) paramMap.put("creator", creator);

        PageBean<Activity> pageBean = activityService.queryPage(paramMap);

        if (from != null && from.equals("combox")) {
            return pageBean.getDatas();
        } else {
            Map<String, Object> result = new HashMap<>();
            result.put("total", pageBean.getTotalsize());
            result.put("rows", pageBean.getDatas());
            return result;
        }
    }


    @PostMapping("/addActivity")
    @ResponseBody
    public AjaxResult addActivity(Activity activity) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            int count = activityService.addActivity(activity);
            if (count > 0) {
                ajaxResult.setSuccess(true);
                ajaxResult.setMessage("添加成功");
            } else {
                ajaxResult.setSuccess(false);
                ajaxResult.setMessage("添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("添加失败");
        }
        return ajaxResult;
    }


    @PostMapping("/editActivity")
    @ResponseBody
    public AjaxResult editActivity(Activity activity) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            int count = activityService.editActivity(activity);
            if (count > 0) {
                ajaxResult.setSuccess(true);
                ajaxResult.setMessage("修改成功");
            } else {
                ajaxResult.setSuccess(false);
                ajaxResult.setMessage("修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("修改失败");
        }
        return ajaxResult;
    }


    @PostMapping("/deleteActivity")
    @ResponseBody
    public AjaxResult deleteActivity(Data data) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            int count = activityService.deleteActivity(data.getIds());
            if (count > 0) {
                ajaxResult.setSuccess(true);
                ajaxResult.setMessage("删除成功");
            } else {
                ajaxResult.setSuccess(false);
                ajaxResult.setMessage("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("删除失败");
        }
        return ajaxResult;
    }
    @GetMapping("/getStatus")
    @ResponseBody
    public Map<String, Object> getStatus(@RequestParam Integer id) {
        Map<String, Object> response = new HashMap<>();
        Integer status = activityService.getStatus(id);

        if (status != null) {
            response.put("success", true);
            response.put("status", status);
        } else {
            response.put("success", false);
            response.put("message", "活动未找到");
        }

        return response;
    }

    // 更新活动状态
    @PostMapping("/updateStatus")
    @ResponseBody
    public Map<String, Object> updateStatus(@RequestBody Map<String, Object> requestBody) {
        Map<String, Object> response = new HashMap<>();

        Integer id = (Integer) requestBody.get("id"); // 活动 ID（假设前端传过来）
        Integer status = (Integer) requestBody.get("status");

        if (id == null || status == null || (status != 0 && status != 1)) {
            response.put("success", false);
            response.put("message", "无效的参数");
            return response;
        }

        boolean updated = activityService.updateStatus(id, status);

        if (updated) {
            response.put("success", true);
            response.put("message", "状态已更新");
        } else {
            response.put("success", false);
            response.put("message", "更新失败");
        }

        return response;
    }
}
